

$(document).ready(function(){
    loadTopics()
    loadDefaultBlog();
});
var token = window.localStorage.getItem("token");
var TYPE_GET = "GET";
var TYPE_POST = "POST";
var getAllTopicByUser = "/blog/getAllTopicByUser?token=" + token;
var saveTopic = "/blog/saveTopic?token=" + token;
var createBlog = "/blog/createBlog?token=" + token;
var saveBlog = "/blog/saveBlog?token=" + token;
var TITLE = "新建标题";
var checkedTopicId;
var checkedBlogId;
function loadTopics() {
    $.ajax({
        url : getAllTopicByUser,
        type : TYPE_GET,
        dataType : 'json',
        async: false,
        success:function(data) {
            if(data.code == 200 ){
                var result = data.dataList;
                $(result).each(function (index, item) {
                    addTopic(item.id, item.topic);
                });
            }else{
                alert("数据异常");
            }
         },
        error : function() {
            alert("网络异常");
        }
    });
}

$(document).on("click", "#save_topic", function() {
    var topic = $("#topic").val();
    var data = {
        topic : topic
    };
    $.ajax({
        url : saveTopic,
        type : TYPE_POST,
        dataType : 'json',
        data : data,
        success:function(data) {
            if(data.code == 200 ){

                addTopic(data.id, topic);

            }else{
                alert("数据异常");
            }
         },
         error : function() {
            alert("网络异常");
         }
    });
})

$(document).on("click", "#create_blog", function() {

    var data = {
        "title" : TITLE,
        "topicId" : checkedTopicId
    }

    $.ajax({
        url : createBlog,
        type : TYPE_POST,
        dataType : 'json',
        data : data,
        success:function(data) {
            if(data.code == 200 ){

                addBlog(data.id, TITLE);

            }else{
                alert("数据异常");
            }
         },
         error : function() {
            alert("网络异常");
         }
    });
})

function addTopic(topicId, topic) {
    var a = "<a class=\"check_topic\" href=\"javascript:;\"></a>";
    var div = "<div class=\"topic_item\"></div>";
    var input = "<input name=\"topicId\" type=\"hidden\"></input>";
    var p = "<p></p>";
    input = $(input).attr("value", topicId)
    div = $(div).append($(input), $(p).text(topic));
    a = $(a).append($(div));
    $("#topics").append($(a));
}

function addBlog(blogId, title){
    var a = "<a class=\"check_blog\" href=\"javascript:;\"></a>";
    var div = "<div class=\"blog_item\"></div>";
    var input = "<input name=\"blogId\" type=\"hidden\"></input>";
    var p = "<p></p>";
    input = $(input).attr("value", blogId)
    div = $(div).append($(input), $(p).text(title));
    a = $(a).append($(div));
    $("#blog_list").append($(a));
}

$(document).on("click", ".check_topic", function(){
    $("#blog_list").empty();
    checkedTopicId = $(this).find("input").val();
    var getAllBlogByTopicId = "/blog/getAllBlogByTopicId?token=" + token;
    getAllBlogByTopicId += "&topicId=" + checkedTopicId;
    $.ajax({
        url : getAllBlogByTopicId,
        contentType : 'application/json',
        type : TYPE_GET,
        async: false,
        dataType : 'json',
        success : function(data) {
            if(data.code == 200 ){
                var result = data.dataList;
                $(result).each(function (index, item) {
                    addBlog(item.id, item.title);
                });

            }else{
                alert("数据异常");
            }

         },
         error : function() {
            alert("网络异常");
         }
    });

})

function loadDefaultBlog() {
    $(".check_topic").first().trigger("click");
    $(".check_blog").first().trigger("click");
}

$(document).on("click", ".check_blog", function(){
    checkedBlogId = $(this).find("input").val();
    var getBlogById = "/blog/getBlogById?token=" + token + "&id=" + checkedBlogId ;
    $.ajax({
        url : getBlogById,
        type : TYPE_GET,
        dataType : 'json',
        success : function(data) {
            if(data.code == 200 ){
                var result = data.result;
                $("#title_input").val(result.title);
                editor.txt.html(result.content);
            }else{
                alert("数据异常");
            }

         },
         error : function() {
            alert("网络异常");
         }
    });

    uploadImgParams(checkedTopicId, checkedBlogId);
})

$(document).on("click", "#blog_save", function(){
    var title = $("#title_input").val();
    var data = {
        blogId : checkedBlogId,
        title : title,
        content : editor.txt.html()
    }
    $.ajax({
        url : saveBlog,
        type : TYPE_POST,
        dataType : 'json',
        data : data,
        success : function(data) {
            if(data.code == 200 ){
                alert("保存成功！");

            }else{
                alert("数据异常");
            }

         },
        error : function() {
            alert("网络异常");
        }
    });

})